package org.example.student_dormitory.dao.impl;

import org.example.student_dormitory.dao.BuildingManagerDao;
import org.example.student_dormitory.entity.Admin;
import org.example.student_dormitory.entity.Building;
import org.example.student_dormitory.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BuildingManagerDaoImpl implements BuildingManagerDao {
    //无条件查询
    @Override
    public List<Building> selectAll(int page, int pageSize) {
        //创建结果集
        List<Building> list = new ArrayList<>();
        String sql = "select b.*,a.name as adminName from building b left join admin a on " +
                "b.adminId = b.id where b.dr=0 order by b.id desc limit ?,?";
        //计算分页数据
        page = (page-1)*pageSize;
        //查询
        List<Map<String, Object>> queryList = DBUtils.queryList(sql, page, pageSize);
        //判断
        if (queryList == null || queryList.size() == 0){
            //没数据
            return null;
        }else {
            //有数据,数据解析
            //获取总数据
            int count = selectCount();
            for (int i = 0; i < queryList.size(); i++) {
                Building building = new Building();
                building.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                building.setName(queryList.get(i).get("name").toString());
                building.setExplain(queryList.get(i).get("explain").toString());
                building.setAdminId(Long.parseLong(queryList.get(i).get("adminId").toString()));
//                building.setAdminName(queryList.get(i).get("adminName").toString());
                building.setDr(Long.parseLong(queryList.get(i).get("dr").toString()));
                building.setTotal_count(count);

                list.add(building);
            }
            return list;
        }
    }
    //有条件查询
    @Override
    public List<Building> selectAll(int page, int pageSize, String key, String value) {
        List<Building> list = new ArrayList<>();
        //拼接搜索关键字
        value = "%" + value + "%";
        page = (page-1)*pageSize;
        //判断用户是否是需要根据管理员名字来搜索
        if ("adminName".equals(key)) {
            //管理员名字搜索
            //g根据名字找id
            String sql1 = "select id from admin where name like ? and dr=1";
            List<Map<String, Object>> queryList = DBUtils.queryList(sql1, value);
            if (queryList == null || queryList.size() == 0){
                return null;
            }else {
//                List<Integer> idList = new ArrayList<>();
                String erg = "";
                for (int i = 0; i < queryList.size(); i++) {
//                    idList.add(Integer.parseInt(queryList.get(i).get("id").toString()));
                    erg += Integer.parseInt(queryList.get(i).get("id").toString());
                    //最后一个值的后面不用拼接逗号
                    if (i != queryList.size()-1) {
                        erg += ",";
                    }
                }
                //根据admin 的 id 找 building 的id
                String sql2 = "select b.*,a.name adminName from building b left join admin a " +
                        " on b.adminId = a.id where a.id in (?) order by b.id desc limit ?,?";

                //查询
                List<Map<String, Object>> resultList = DBUtils.queryList(sql2, erg, page, pageSize);
                //判断
                if (resultList == null || resultList.size() == 0){
                    //没数据
                    return null;
                }else {
                    //有数据,数据解析
                    //获取总数据
                    String sql3 = "select count(*) from building where adminId in (?)";
                    Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql3,erg);

                    int count = Integer.parseInt(stringObjectMap.get("count(*)").toString());
                    for (int i = 0; i < resultList.size(); i++) {
                        Building building = new Building();
                        building.setId(Long.parseLong(resultList.get(i).get("id").toString()));
                        building.setName(resultList.get(i).get("name").toString());
                        building.setExplain(resultList.get(i).get("explain").toString());
                        building.setAdminId(Long.parseLong(resultList.get(i).get("adminId").toString()));
                        building.setDr(Long.parseLong(resultList.get(i).get("dr").toString()));
                        building.setTotal_count(count);

                        list.add(building);
                    }
                    return list;
                }
            }
        }else {

            String sql = "select b.*,a.name as adminName from building b left join admin a on " +
                    "b.adminId = b.id where b.`"+ key +"` like ? and b.dr=0 or b.dr=1 order by b.id desc limit ?,?";

            //查询
            List<Map<String, Object>> queryList = DBUtils.queryList(sql, value, page, pageSize);
            //判断
            if (queryList == null || queryList.size() == 0){
                //没数据
                return null;
            }else {
                //有数据,数据解析
                //获取总数据
                int count = selectCount(key, value);
                for (int i = 0; i < queryList.size(); i++) {
                    Building building = new Building();
                    building.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                    building.setName(queryList.get(i).get("name").toString());
                    building.setExplain(queryList.get(i).get("explain").toString());
                    building.setAdminId(Long.parseLong(queryList.get(i).get("adminId").toString()));
                    building.setDr(Long.parseLong(queryList.get(i).get("dr").toString()));
                    building.setTotal_count(count);

                    list.add(building);
                }
                return list;
            }
        }
    }
    //无条件总数查询
    @Override
    public int selectCount() {
        String sql = "select count(*) from building where dr=0";
        Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql);
        return Integer.parseInt(stringObjectMap.get("count(*)").toString());
    }
    //有条件总数查询
    @Override
    public int selectCount(String key, String value) {
        String sql = "select count(*) from building where `"+ key +"` like ? and dr=0";
        Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql,value);
        return Integer.parseInt(stringObjectMap.get("count(*)").toString());
    }

    @Override
    public List<Admin> selectAllAdmin() {
        List<Admin> list = new ArrayList<>();
        //查询所有管理员信息
        String sql = "select * from admin where dr = 1";
        List<Map<String, Object>> queryList = DBUtils.queryList(sql);
        if (queryList == null || queryList.size() == 0){
            return null;
        }else {
            for (int i = 0; i < queryList.size(); i++) {
                Admin admin = new Admin();
                admin.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                admin.setName(queryList.get(i).get("name").toString());
                list.add(admin);
            }
            return list;
        }
    }

    @Override
    public boolean insert(Building building) {
        //插入楼宇信息
        String sql = "insert into building(name,`explain`,adminId,dr) values(?,?,?,?)";
        boolean insert = false;
        try {
            insert = DBUtils.insert(sql, building.getName(), building.getExplain(), building.getAdminId(), building.getDr());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return insert;
    }

    @Override
    public boolean update(Building building) {
        String sql = "update building set `explain`=? where id=?";
        try {
            System.out.println("在Dao中准备返回结果");
            return DBUtils.update(sql,building.getExplain(),building.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean deleteById(int id) {
        String sql = "update building set dr=1 where id=?";
        try {
            return DBUtils.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
